﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Supermarket.Bll;

namespace Supermarket.Web.CashierPages
{
    public partial class SupplyProducts : System.Web.UI.Page
    {
        public static decimal ProductPrice { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CashierPages/SupplyProducts.aspx");
        }

        protected void ButtonBuy_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Response.Redirect(String.Format("~/CashierPages/AddToCart.aspx?productId={0}&quantity={1}&productPrice={2}", hfId.Value, TextBoxQuantity.Text, TextBoxProductPrice.Text));
            }
            else
            {
                LabelError.Text = string.Format("Продажна цена: {0:0.00}", ProductPrice);
                LinkButtonForceSupply.Visible = true;
            }
        }

        protected void GridViewProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

            hfId.Value = GridViewProducts.DataKeys[row.RowIndex].Value.ToString();
            LabelProductName.Text = GridViewProducts.Rows[row.RowIndex].Cells[0].Text;
            ProductPrice = decimal.Parse(GridViewProducts.Rows[row.RowIndex].Cells[1].Text);
            LabelMeasureCode.Text = GridViewProducts.Rows[row.RowIndex].Cells[2].Text;
            MultiViewSupplies.ActiveViewIndex = 1;
        }

        protected void LinkButtonSupply_Click(object sender, EventArgs e)
        {
            MultiViewSupplies.ActiveViewIndex = 0;
            Response.Redirect("CheckOutSupply.aspx");
        }

        protected void PriceProductCheck(object source, ServerValidateEventArgs args)
        {
            decimal supplyPrice = decimal.Parse(args.Value);
            if (supplyPrice > ProductPrice)
            {
                args.IsValid = false;                
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void LinkButtonForceSupply_Click(object sender, EventArgs e)
        {
            Response.Redirect(String.Format("~/CashierPages/AddToCart.aspx?productId={0}&quantity={1}&productPrice={2}", hfId.Value, TextBoxQuantity.Text, TextBoxProductPrice.Text));
        }
    }
}